package xyz.yuzegod.megawalls.game;

import org.bukkit.Bukkit;
import org.bukkit.block.Block;
import xyz.yuzegod.megawalls.MegaWalls;
import xyz.yuzegod.megawalls.tellraw.Tellraw;

import java.util.ArrayList;
import java.util.List;

public class GameParty
{
    private Game game;
    private GamePlayer leader;
    private List<GamePlayer> players;
    private List<GamePlayer> inviting;
    
    public GameParty(final Game game, final GamePlayer leader) {
        this.leader = null;
        this.players = null;
        this.inviting = null;
        this.game = game;
        this.leader = leader;
        this.players = new ArrayList<GamePlayer>();
        this.inviting = new ArrayList<GamePlayer>();
        this.players.add(leader);
        game.addParty(this);
    }
    
    public GamePlayer getLeader() {
        return this.leader;
    }
    
    public boolean isLeader(final GamePlayer p) {
        return p.equals(this.leader);
    }
    
    public List<GamePlayer> getPlayers() {
        return this.players;
    }
    
    public boolean isInTeam(final GamePlayer p) {
        for (final GamePlayer player : this.players) {
            if (player.equals(p)) {
                return true;
            }
        }
        return false;
    }
    
    public boolean addPlayer(final GamePlayer p) {
        if (this.isFull() || this.isInTeam(p)) {
            return false;
        }
        this.players.add(p);
        return true;
    }
    
    public boolean removePlayer(final GamePlayer p) {
        if (!this.isInTeam(p)) {
            return false;
        }
        this.players.remove(p);
        if (this.players.isEmpty()) {
            this.game.removeParty(this);
            return true;
        }
        if (this.isLeader(p)) {
            for (final GamePlayer player : this.players) {
                if (player.isOnline()) {
                    this.leader = player;
                    this.broadcast("§a队伍队长离开了游戏,§e" + player.getName() + "§a被提升为队长！");
                    break;
                }
            }
            return true;
        }
        this.broadcast("§e" + p.getName() + "§a离开了队伍！");
        return true;
    }
    
    public boolean isFull() {
        return this.players.size() >= 4;
    }
    
    public boolean isInviting(final GamePlayer p) {
        return this.inviting.contains(p);
    }
    
    public void invite(final GamePlayer p) {
        if (this.isFull() || this.isInviting(p)) {
            return;
        }
        this.inviting.add(p);
        this.leader.getPlayer().sendMessage("§a你邀请了§e" + p.getName() + "§a， 该玩家有§e30§a秒的时间来同意邀请！");
        Tellraw.create("§e" + this.leader.getName() + "§a邀请你加入队伍， 你有§e30§a秒的时间来同意邀请！").then("§b[点击同意]").command("/party accept " + this.leader.getName()).send(p.getPlayer());
        Bukkit.getScheduler().runTaskLater(MegaWalls.getInstance(), (Runnable)new Runnable() {
            @Override
            public void run() {
                if (GameParty.this.inviting.contains(p)) {
                    GameParty.this.inviting.remove(p);
                    GameParty.this.leader.getPlayer().sendMessage("§a你对§e" + p.getName() + "§a的邀请已经失效！");
                    p.sendMessage("§e" + p.getName() + "§a对你的邀请已失效！");
                }
            }
        }, 600L);
    }
    
    public void acceptInvite(final GamePlayer p) {
        if (!this.isInviting(p)) {
            return;
        }
        if (this.addPlayer(p)) {
            this.inviting.remove(p);
            this.broadcast("§e" + p.getName() + "§a加入了队伍！");
        }
    }
    
    public void broadcast(final String message) {
        for (final GamePlayer player : this.players) {
            player.sendMessage(message);
        }
    }
    
    public List<GamePlayer> getMates(final GamePlayer gamePlayer) {
        final List<GamePlayer> players = new ArrayList<GamePlayer>();
        for (final GamePlayer player : this.getPlayers()) {
            if (!player.equals(gamePlayer)) {
                players.add(player);
            }
        }
        return players;
    }
    
    public boolean isProtectedBlock(final Block block) {
        for (final GamePlayer gamePlayer : this.players) {
            if (gamePlayer.isProtectedBlock(block)) {
                return true;
            }
        }
        return false;
    }
}
